<?php
/**
 * 撤销交易
 * @author Administrator
 *
 */
class cancel extends CControllerMerchantBase{
	
	public function change_list()
	{
		$this->_title="撤销交易操作";
		
		$icModel = new integralChangeModel();
		$dateBefore = date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s")." -1 hour"));
		$where = array();
		
		$s_type=$this->getRequestData("type","get");
		$s_v=$this->getRequestData("sv","get");

		if($s_type && $s_v)
		{
			$this->setViewParam("type", $s_type);
			$this->setViewParam("s_v", $s_v);
			if($s_type=="card")
			{
				$where["supplementary_card"] = $s_v;
			}
			else{
				$where["owner_name"] = $s_v;
			}
		}

		$data = $icModel->selectDataSetByWhere(
			"t_integral_change c, t_owner o",
			"c_id,pay_id,cost_money,cost_type,cost_time,owner_card,cost_shop_before_money,cost_shop_after_money,cost_money_t,o.owner_name,is_cancel",
			$where,"c.owner_id=o.owner_id and shop_id=".$this->_shopID." and cost_type<3 and cost_time>to_date('".$dateBefore."','yyyy-mm-dd hh24:mi:ss')","c_id desc"
		);

		foreach($data as &$item)
		{
			$payID = $item["pay_id"];
			$payID = substr($payID,0,8)." ".substr($payID,8);
			$item["pay_id"] = $payID;
		}
		$this->setViewParam("userHtml","<span class='user_html color_red font12'>*撤销交易操作只限1小时内发生的交易</span>");
		$this->render("cancel",array("url"=>"/merchant/cancel/change_list", "data"=>$data));
	}
	
	public function op_cancel()
	{
		$cid=$this->getRequestData("cid");
		$pwd=$this->getRequestData("pwd");
		
		$pwd = CCommon::pwdMd5($pwd);
		
		$shopModel = new shopModel();
		if($shopModel->checkPayPwd($this->_shopID,$pwd))
		{
			$dateBefore = date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s")." -1 hour"));
			$cInfo = $shopModel->selectByWhere("t_integral_change","*",array("c_id"=>$cid,"shop_id"=>$this->_shopID),"cost_time>to_date('".$dateBefore."','yyyy-mm-dd hh24:mi:ss')");
			if(!$cInfo)
			{
				echo -2; //记录不存在
				exit;
			}
			if($cInfo["cost_type"]==3 || $cInfo["is_cancel"]==1)
			{
				echo -3; //撤销的交易不能再撤销
				exit;
			}
			
			$owModel = new ownerShopModel();
			if($owModel->cancelPay($cInfo))
			{
				echo 1;
			}
			else{
				echo -4;
			}
		}
		else{
			echo -1; //密码不正确
		}
		
		exit;
	}
}